package models.data;

import models.general.vKennariHoparOnn;
import models.general.vNemandiAfangarOnn;
import play.data.validation.Constraints;
import play.db.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: olgak
 * Date: 2.5.2013
 * Time: 13:49
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class Stundatoflur_NK extends Model {
    @Id
    @Constraints.Required
    private Integer id;
    @Constraints.Required
    private String dagur;
    @Constraints.Required
    private String onn_id;
    @Constraints.Required
    private String hopur_id;
    /*TIMESLOT
        1=8:30-9:15
        2=9:20-10:05
        3=10:20-11:05
        4=11:10-11:55
        5=12:20-13:05
        6=13:10-13:55
        7=14:00-14:45
        8=14:55-15:40
     */
    @Constraints.Required
    private Integer timeslot;
    @Constraints.Required
    private String kennari_upphafsstafir; //Initials of teacher
    @Constraints.Required
    private String stofa;
    @Constraints.Required
    private String afangi_id;
    private String namsgrein_afanganr;

    public Stundatoflur_NK(String dagur, String onn_id, String hopur_id, String kennari, Integer timeslot, String stofa, String afangi_id, String namsgrein_afanganr) {
        this.dagur = dagur;
        this.onn_id = onn_id;
        this.hopur_id = hopur_id;
        this.kennari_upphafsstafir = kennari;
        this.timeslot = timeslot;
        this.stofa = stofa;
        this.id = createId();
        this.afangi_id = afangi_id;
        this.namsgrein_afanganr = namsgrein_afanganr;
    }

    public Stundatoflur_NK() {
    }

    public Stundatoflur_NK(int timeslot, String namsgrein_afanganr, String dagur) {
        this.timeslot = timeslot;
        this.namsgrein_afanganr = namsgrein_afanganr;
        this.dagur = dagur;

    }

    public static Stundatoflur_NK create(String dagur,String onn_id, String hopur_id, String kennari, Integer timeslot, String stofa, String afangi_id, String namsgrein_afanganr){
        Stundatoflur_NK stundatofluFaersla= new Stundatoflur_NK(dagur, onn_id, hopur_id, kennari, timeslot, stofa ,afangi_id, namsgrein_afanganr);
        stundatofluFaersla.save();
        return stundatofluFaersla;

    }

    public static Finder<Integer,Stundatoflur_NK> find = new Finder<Integer,Stundatoflur_NK>(
            Integer.class, Stundatoflur_NK.class
    );

    public static List<List<Stundatoflur_NK>> getStundataflaKennara(List<vKennariHoparOnn> courses){
        List<Stundatoflur_NK> monday = new ArrayList<Stundatoflur_NK>(); //the schedule for monday
        List<Stundatoflur_NK> tuesday = new ArrayList<Stundatoflur_NK>();   //schedule for tuesdday
        List<Stundatoflur_NK> wednesday = new ArrayList<Stundatoflur_NK>(); //schedule for wednesday
        List<Stundatoflur_NK> thursday = new ArrayList<Stundatoflur_NK>();   //schedule for thursday
        List<Stundatoflur_NK> friday = new ArrayList<Stundatoflur_NK>();  //schedule for friday
        List<List<Stundatoflur_NK>> allWeek= new ArrayList<List<Stundatoflur_NK>>(); //the returnlist which will have all the days together, list of the days
        //here we initialize all elements in the list so the size will be 8
        //we do this to make sure the size will be exactly 8 because we need that when we are showing the courses in the views
        for(int i=1;i<9;i++){
            monday.add(new Stundatoflur_NK(i,"","manudagur"));
        }
        for(int j=9;j<17;j++){
            tuesday.add(new Stundatoflur_NK(j, "", "thridjudagur"));
        }
        for(int k=17;k<25;k++){
            wednesday.add(new Stundatoflur_NK(k, "", "midvikudagur"));
        }
        for(int p=25;p<33;p++){
            thursday.add(new Stundatoflur_NK(p, "", "fimmtudagur"));
        }
        for(int b=33;b<41;b++){
            friday.add(new Stundatoflur_NK(b,"", "fostudagur"));
        }


        //foreach course the user is registered for
        for (vKennariHoparOnn course:courses){
            //find the time when those courses are taught and add them to the list "mm"
            List<Stundatoflur_NK> mm = find.where().eq("onn_id", course.onn_id).where().eq("hopur_id", course.hopur).where().eq("afangi_id", course.afangi_id).findList();
            //foreach time the course is taught, we add the course/time to the right list based on what day it is taught
            for (Stundatoflur_NK single:mm){
                if(single.getDagur().equals("manudagur")){
                    monday.set(single.getTimeslot()-1, single);
                }
                if(single.getDagur().equals("thridjudagur")){
                    tuesday.set(single.getTimeslot() - 1, single);
                }
                if(single.getDagur().equals("midvikudagur")){
                    wednesday.set(single.getTimeslot() - 1, single);
                }
                if(single.getDagur().equals("fimmtudagur")){
                    thursday.set(single.getTimeslot()-1, single);
                }
                if(single.getDagur().equals("fostudagur")){
                    friday.set(single.getTimeslot()-1, single);
                }
            }
        }

        //add all the lists to the list allWeek
        allWeek.add(monday);
        allWeek.add(tuesday);
        allWeek.add(wednesday);
        allWeek.add(thursday);
        allWeek.add(friday);

        return allWeek;

    }





    //Returns a list of lists to show the course scheduale for each day
    //returns seperate list for the classes each day
    //if isStudent == true, we will get the courses for a student, otherswise, we wil lget the courses for a teacher
    public static List<List<Stundatoflur_NK>> getStudentStundatafla(List<vNemandiAfangarOnn> courses){
        List<Stundatoflur_NK> monday = new ArrayList<Stundatoflur_NK>(); //the schedule for monday
        List<Stundatoflur_NK> tuesday = new ArrayList<Stundatoflur_NK>();   //schedule for tuesdday
        List<Stundatoflur_NK> wednesday = new ArrayList<Stundatoflur_NK>(); //schedule for wednesday
        List<Stundatoflur_NK> thursday = new ArrayList<Stundatoflur_NK>();   //schedule for thursday
        List<Stundatoflur_NK> friday = new ArrayList<Stundatoflur_NK>();  //schedule for friday
        List<List<Stundatoflur_NK>> allWeek= new ArrayList<List<Stundatoflur_NK>>(); //the returnlist which will have all the days together, list of the days
        //here we initialize all elements in the list so the size will be 8
        //we do this to make sure the size will be exactly 8 because we need that when we are showing the courses in the views
        for(int i=0;i<8;i++){
            monday.add(new Stundatoflur_NK(i+1,"","manudagur"));
            tuesday.add(new Stundatoflur_NK(i+1, "", "thridjudagur"));
            wednesday.add(new Stundatoflur_NK(i+1, "", "midvikudagur"));
            thursday.add(new Stundatoflur_NK(i+1, "", "fimmtudagur"));
            friday.add(new Stundatoflur_NK(i+1,"", "fostudagur"));
        }

            //foreach course the user is registered for
            for (vNemandiAfangarOnn course:courses){
                //find the time when those courses are taught and add them to the list "mm"
                List<Stundatoflur_NK> mm = find.where().eq("onn_id", course.onn_id).where().eq("hopur_id", course.hopur).where().eq("afangi_id", course.afangi_id).findList();
                //foreach time the course is taught, we add the course/time to the right list based on what day it is taught
                for (Stundatoflur_NK single:mm){
                    if(single.getDagur().equals("manudagur")){
                        monday.set(single.getTimeslot()-1, single);
                    }
                    if(single.getDagur().equals("thridjudagur")){
                        tuesday.set(single.getTimeslot() - 1, single);
                    }
                    if(single.getDagur().equals("midvikudagur")){
                        wednesday.set(single.getTimeslot() - 1, single);
                    }
                    if(single.getDagur().equals("fimmtudagur")){
                        thursday.set(single.getTimeslot()-1, single);
                    }
                    if(single.getDagur().equals("fostudagur")){
                        friday.set(single.getTimeslot()-1, single);
                    }
                }
            }

        //add all the lists to the list allWeek
        allWeek.add(monday);
        allWeek.add(tuesday);
        allWeek.add(wednesday);
        allWeek.add(thursday);
        allWeek.add(friday);

        return allWeek;
    }

    public static Integer createId(){
        List<Object> stundatoflur = Stundatoflur_NK.find.findIds();

        Integer max = 1;
        for(Object verk : stundatoflur){
            if(Integer.valueOf(verk.toString()) > max) {
                max = Integer.valueOf(verk.toString());
            }
        }
        return (max+1);
    }

    public String getDagur() {
        return dagur;
    }

    public void setDagur(String dagur) {
        this.dagur = dagur;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOnn_id() {
        return onn_id;
    }

    public void setOnn_id(String onn_id) {
        this.onn_id = onn_id;
    }

    public String getHopur_id() {
        return hopur_id;
    }

    public void setHopur_id(String hopur_id) {
        this.hopur_id = hopur_id;
    }

    public Integer getTimeslot() {
        return timeslot;
    }

    public void setTimeslot(Integer timeslot) {
        this.timeslot = timeslot;
    }

    public String getKennari_upphafsstafir() {
        return kennari_upphafsstafir;
    }

    public void setKennari_upphafsstafir(String kennari_upphafsstafir) {
        this.kennari_upphafsstafir = kennari_upphafsstafir;
    }

    public String getStofa() {
        return stofa;
    }

    public void setStofa(String stofa) {
        this.stofa = stofa;
    }

    public String getAfangi_id() {
        return afangi_id;
    }

    public void setAfangi_id(String afangi_id) {
        this.afangi_id = afangi_id;
    }

    public String getNamsgrein_afanganr() {
        return namsgrein_afanganr;
    }

    public void setNamsgrein_afanganr(String namsgrein_afanganr) {
        this.namsgrein_afanganr = namsgrein_afanganr;
    }
}
